package com.eedi.framework.event.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.event.enums.SysEventConsumeStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.event.controller.vo.SysEventConsumerCreateReq;
import com.eedi.framework.event.controller.vo.SysEventConsumerQueryReq;
import com.eedi.framework.event.controller.vo.SysEventConsumerResp;
import com.eedi.framework.event.convert.SysEventConsumerConvert;
import com.eedi.framework.event.dal.dataobject.SysEventConsumerDO;
import com.eedi.framework.mybatis.core.dataobject.BaseDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import jodd.util.StringUtil;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Mapper
public interface SysEventConsumerMapper extends BaseMapperX<SysEventConsumerDO> {

    default void create(SysEventConsumerCreateReq req) {
        if (ObjectUtil.isEmpty(req)) {
            return;
        }
        SysEventConsumerDO createDo = SysEventConsumerConvert.INSTANCE.convert(req);
        insert(createDo);
    }

    default PageResult<SysEventConsumerResp> getConsumer(SysEventConsumerQueryReq req) {
        PageResult<SysEventConsumerDO> result = selectPage(req, new LambdaQueryWrapperX<SysEventConsumerDO>()
                .eqIfPresent(SysEventConsumerDO::getSysEventConsumeStatus,req.getSysEventConsumeStatus())
                .orderByAsc(BaseDO::getCreateTime));
        return result.convertPage(SysEventConsumerConvert.INSTANCE::convert);
    }

   List< SysEventConsumerResp> getConsumerOnCreated(Collection<String> sysEventConsumerIds);

    default void updateStatusToQueue(Collection<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        List<SysEventConsumerDO> consumerDOList = new ArrayList<>();
        for (String id : ids) {
            consumerDOList.add(new SysEventConsumerDO().setSysEventConsumerId(id)
                    .setSysEventConsumeStatus(SysEventConsumeStatusEnum.QUEUE_IN));
        }
        updateBatch(consumerDOList);
    }


    default void updateStatusToHandle(String id) {
        if (StringUtil.isBlank(id)) {
            return;
        }
        SysEventConsumerDO eventConsumer=new SysEventConsumerDO();
        eventConsumer.setSysEventConsumerId(id);
        eventConsumer.setSysEventConsumeStatus(SysEventConsumeStatusEnum.HANDLE);
        updateById(eventConsumer);
    }

    default void updateStatusToException(String id,String msg) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        updateById(new SysEventConsumerDO()
                .setSysEventConsumerId(id)
                .setSysEventConsumeMsg(msg)
                .setSysEventConsumeStatus(SysEventConsumeStatusEnum.EXCEPTION));
    }

    default void updateStatusToSubmitted(String id,String msg) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        updateById(new SysEventConsumerDO()
                .setSysEventConsumerId(id)
                .setSysEventMsg(msg)
                .setSysEventConsumeStatus(SysEventConsumeStatusEnum.SUBMITTED));
    }

    default void create(Collection<SysEventConsumerCreateReq> reqs) {
        if (CollUtil.isEmpty(reqs)) {
            return;
        }
        List<SysEventConsumerDO> list = reqs.stream().map(SysEventConsumerConvert.INSTANCE::convert).toList();
        list.forEach(x->{
            x.setSysEventConsumeStatus(SysEventConsumeStatusEnum.CREATED);
        });
        insertBatch(list);
    }
}
